package cn.edu.sptc.sms_lioayu.controller;

import cn.edu.sptc.sms_lioayu.entity.Major;
import cn.edu.sptc.sms_lioayu.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/major")
public class MajorController {
    // 控制层controller调用服务层service
    @Autowired
    private MajorService majorService;

    @GetMapping("/query/{id}")
    public Map<String,Object> queryMajorById(@PathVariable String id){
        Map<String,Object> map = new HashMap();
        map.put("code", "200");
        map.put("message", "成功");
        map.put("data", majorService.selectByID(Long.parseLong(id)));
        return map;
    }

    @GetMapping("/query")
    public Map<String,Object> queryMajorByIdParam(@RequestParam String id){
        Map<String,Object> map = new HashMap();
        map.put("code", "200");
        map.put("message", "成功");
        map.put("data", majorService.selectByID(Long.parseLong(id)));
        return map;
    }

    //json格式提交
    @PostMapping(value = "/save", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> insert(@RequestBody Major major) {
        major.setCreateTime(new Date());
        major.setUpdateTime(new Date());
        Map<String, Object> map = new HashMap();
        boolean flag = majorService.insert(major);
        if (flag) {
            map.put("code", "200");
            map.put("message", "插入成功");
        } else {
            map.put("code", "201");
            map.put("message", "插入失败");
        }
        return map;
    }


    //表单提交
    @PostMapping(value = "/save", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Map<String, Object> insertByForm(Major major) {
        major.setCreateTime(new Date());
        major.setUpdateTime(new Date());
        Map<String, Object> map = new HashMap();
        boolean flag = majorService.insert(major);
        if (flag) {
            map.put("code", "200");
            map.put("message", "插入成功");
        } else {
            map.put("code", "201");
            map.put("message", "插入失败");
        }
        return map;
    }
//更新

    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> updateByJSON(@RequestBody Major major) {
        major.setUpdateTime(new Date());
        Map<String, Object> map = new HashMap();
        boolean flag = majorService.update(major);
        if (flag) {
            map.put("code", "200");
            map.put("message", "更新成功");
        } else {
            map.put("code", "201");
            map.put("message", "更新失败");
        }
        return map;
    }
    //删除
    @PostMapping("/delete/{id}")
    public Map<String, Object> deleteMajorByIdParam(@PathVariable String id) {
        //新建一个map
        Map<String, Object> map = new HashMap();
        map.put("code", "200");
        map.put("message", "成功");
        map.put("data", majorService.delete(Long.valueOf(id)));
        return map;
    }
}


